/// Elegance控件库的单元格控件
///
/// 用于展示列表数据，如个人中心、设置页面等

// ignore_for_file: dangling_library_doc_comments

import 'package:flutter/material.dart';

import '../../core/elegance_theme.dart';
import 'e_text.dart';

/// 箭头方向枚举
enum ArrowDirection {
  /// 向右
  right,

  /// 向左
  left,

  /// 向上
  up,

  /// 向下
  down,
}

/// 单元格组件
class ECell extends StatelessWidget {
  /// 创建单元格组件
  const ECell({
    super.key,
    this.title,
    this.label,
    this.value,
    this.icon,
    this.disabled = false,
    this.border = true,
    this.center = true,
    this.url,
    this.linkType = 'navigateTo',
    this.clickable = false,
    this.isLink = false,
    this.required = false,
    this.rightIcon = 'arrow-right',
    this.arrowDirection = ArrowDirection.right,
    this.iconStyle,
    this.rightIconStyle,
    this.titleStyle,
    this.size,
    this.stop = true,
    this.name,
    this.onTap,
    this.titleSlot,
    this.valueSlot,
    this.iconSlot,
    this.rightIconSlot,
    this.labelSlot,
  });

  /// 左侧标题
  final String? title;

  /// 标题下方的描述信息
  final String? label;

  /// 右侧的内容
  final String? value;

  /// 左侧图标名称，或者图片链接
  final String? icon;

  /// 是否禁用cell
  final bool disabled;

  /// 是否显示下边框
  final bool border;

  /// 内容是否垂直居中
  final bool center;

  /// 点击后跳转的URL地址
  final String? url;

  /// 链接跳转的方式
  final String linkType;

  /// 是否开启点击反馈
  final bool clickable;

  /// 是否展示右侧箭头并开启点击反馈
  final bool isLink;

  /// 是否显示表单状态下的必填星号
  final bool required;

  /// 右侧的图标箭头
  final String rightIcon;

  /// 右侧箭头的方向
  final ArrowDirection arrowDirection;

  /// 左侧图标样式
  final TextStyle? iconStyle;

  /// 右侧箭头图标的样式
  final TextStyle? rightIconStyle;

  /// 标题的样式
  final TextStyle? titleStyle;

  /// 单元格的大小
  final String? size;

  /// 点击cell是否阻止事件传播
  final bool stop;

  /// 标识符，用于在click事件中进行返回
  final dynamic name;

  /// 点击事件回调
  final Function(dynamic name)? onTap;

  /// 自定义左侧标题部分的内容
  final Widget? titleSlot;

  /// 自定义右侧标题部分的内容
  final Widget? valueSlot;

  /// 自定义左侧的图标
  final Widget? iconSlot;

  /// 自定义右侧图标内容
  final Widget? rightIconSlot;

  /// 自定义label内容
  final Widget? labelSlot;

  @override
  Widget build(BuildContext context) {
    // 获取主题
    final theme = EleganceTheme.of(context);
    final textRegularColor = theme.textRegularColor;
    final textSecondaryColor = theme.textSecondaryColor;
    final borderColor = theme.borderColor;
    final spacingM = theme.spacingM;
    final spacingS = theme.spacingS;
    final spacingXS = theme.spacingXS;

    // 计算内容高度
    double contentHeight = 50.0;
    if (size == 'large') {
      contentHeight = 60.0;
    }

    // 构建左侧图标
    Widget? iconWidget;
    if (iconSlot != null) {
      iconWidget = iconSlot;
    } else if (icon != null) {
      // 这里简单处理，实际项目中可能需要更复杂的图标处理逻辑
      iconWidget = Container(
        margin: EdgeInsets.only(right: spacingS),
        width: 20,
        height: 20,
        child: Icon(_getIconData(icon!), size: 20, color: textRegularColor),
      );
    }

    // 构建标题
    Widget? titleWidget;
    if (titleSlot != null) {
      titleWidget = titleSlot;
    } else if (title != null) {
      titleWidget = Row(
        children: [
          EText(
            title!,
            size: titleStyle?.fontSize ?? theme.textStyle?.fontSize,
            color: titleStyle?.color ?? theme.textPrimaryColor,
            bold: titleStyle?.fontWeight == FontWeight.bold,
          ),
          if (required)
            Padding(
              padding: EdgeInsets.only(left: spacingXS),
              child: EText('*', color: theme.errorColor),
            ),
        ],
      );
    }

    // 构建描述信息
    Widget? labelWidget;
    if (labelSlot != null) {
      labelWidget = labelSlot;
    } else if (label != null) {
      labelWidget = EText(
        label!,
        size: 12,
        color: textSecondaryColor,
        margin: EdgeInsets.only(top: spacingXS),
      );
    }

    // 构建左侧内容
    Widget? leftContent;
    if (titleWidget != null || labelWidget != null) {
      leftContent = Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: center
            ? MainAxisAlignment.center
            : MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          if (titleWidget != null) titleWidget,
          if (labelWidget != null) labelWidget,
        ],
      );
    }

    // 构建右侧内容
    Widget? rightContent;
    if (valueSlot != null) {
      rightContent = valueSlot;
    } else if (value != null) {
      rightContent = EText(
        value!,
        color: textSecondaryColor,
        align: ETextAlign.right,
      );
    }

    // 构建右侧图标
    Widget? rightIconWidget;
    if (rightIconSlot != null) {
      rightIconWidget = rightIconSlot;
    } else if (isLink) {
      rightIconWidget = Icon(
        _getArrowIconData(),
        size: 14,
        color: textSecondaryColor,
      );
    }

    // 构建右侧整体内容
    Widget? rightWidgets;
    if (rightContent != null || rightIconWidget != null) {
      rightWidgets = Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          if (rightContent != null) rightContent,
          if (rightIconWidget != null)
            Padding(
              padding: EdgeInsets.only(left: spacingS),
              child: rightIconWidget,
            ),
        ],
      );
    }

    // 构建单元格内容
    Widget cellContent = Container(
      height: contentHeight,
      padding: EdgeInsets.symmetric(horizontal: spacingM),
      decoration: BoxDecoration(
        border: border
            ? Border(bottom: BorderSide(color: borderColor, width: 0.5))
            : null,
      ),
      child: Row(
        crossAxisAlignment: center
            ? CrossAxisAlignment.center
            : CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            crossAxisAlignment: center
                ? CrossAxisAlignment.center
                : CrossAxisAlignment.start,
            children: [
              if (iconWidget != null) iconWidget,
              if (leftContent != null) leftContent,
            ],
          ),
          if (rightWidgets != null) rightWidgets,
        ],
      ),
    );

    // 添加点击事件和涟漪效果
    if ((isLink || clickable) && !disabled) {
      return Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: () {
            if (stop) {
              // 阻止事件传播
            }
            onTap?.call(name);
            // 这里可以添加路由跳转逻辑
          },
          borderRadius: BorderRadius.zero,
          splashColor: Theme.of(context).splashColor.withOpacity(0.3),
          highlightColor: Theme.of(context).highlightColor.withOpacity(0.1),
          child: cellContent,
        ),
      );
    }

    return cellContent;
  }

  // 获取图标数据
  IconData _getIconData(String iconName) {
    // 这里只是简单的映射，实际项目中可能需要更复杂的图标处理逻辑
    switch (iconName) {
      case 'arrow-right':
        return Icons.arrow_forward_ios;
      case 'arrow-left':
        return Icons.arrow_back_ios;
      case 'arrow-up':
        return Icons.arrow_upward;
      case 'arrow-down':
        return Icons.arrow_downward;
      default:
        return Icons.info_outline;
    }
  }

  // 获取箭头图标数据
  IconData _getArrowIconData() {
    switch (arrowDirection) {
      case ArrowDirection.left:
        return Icons.arrow_back_ios;
      case ArrowDirection.up:
        return Icons.arrow_upward;
      case ArrowDirection.down:
        return Icons.arrow_downward;
      case ArrowDirection.right:
      default:
        return Icons.arrow_forward_ios;
    }
  }
}

/// 单元格分组组件
class ECellGroup extends StatelessWidget {
  /// 创建单元格分组组件
  const ECellGroup({
    super.key,
    this.title,
    this.border = true,
    this.customStyle,
    required this.children,
  });

  /// 分组标题
  final String? title;

  /// 是否显示外边框
  final bool border;

  /// 用户自定义外部样式
  final TextStyle? customStyle;

  /// 子组件，通常是ECell组件
  final List<Widget> children;

  @override
  Widget build(BuildContext context) {
    // 获取主题
    final theme = EleganceTheme.of(context);
    final surfaceColor = theme.surfaceColor;
    final borderColor = theme.borderColor;
    final spacingM = theme.spacingM;
    final spacingS = theme.spacingS;

    // 构建标题
    Widget? titleWidget;
    if (title != null && title!.isNotEmpty) {
      titleWidget = Padding(
        padding: EdgeInsets.fromLTRB(spacingM, spacingM, spacingM, spacingS),
        child: EText(
          title!,
          size: customStyle?.fontSize ?? 12,
          color: customStyle?.color ?? theme.textSecondaryColor,
          bold: customStyle?.fontWeight == FontWeight.bold,
        ),
      );
    }

    // 构建内容
    Widget content = Container(
      decoration: BoxDecoration(
        color: surfaceColor,
        border: border
            ? Border(
                top: BorderSide(color: borderColor, width: 0.5),
                bottom: BorderSide(color: borderColor, width: 0.5),
              )
            : null,
      ),
      child: Column(children: children),
    );

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [if (titleWidget != null) titleWidget, content],
    );
  }
}
